package com.cloudeasy.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.user.UserInfoVO;
import com.cloudeasy.core.bean.JwtParser;
import com.cloudeasy.user.util.IPageUtil;
import com.cloudeasy.user.dto.UserInfoDTO;
import com.cloudeasy.user.field.UserInfoField;
import com.cloudeasy.user.po.UserInfo;
import com.cloudeasy.user.service.UserInfoService;
import com.cloudeasy.user.util.UserInfoUtil;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/info")
public class UserInfoController {
    private final UserInfoService userInfoService;

    private final JwtParser jwtParser;

    public UserInfoController(UserInfoService userInfoService, JwtParser jwtParser) {
        this.userInfoService = userInfoService;
        this.jwtParser = jwtParser;
    }

    /**
     * 获取当前账号用户信息
     *
     * @param authentication {@link Authentication}
     * @return {@link Result<UserInfo>}
     */
    @GetMapping
    public Result<UserInfo> me(Authentication authentication) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(userInfoService.getById(userId));
    }

    /**
     * 更新当前用户信息
     *
     * @param authentication {@link Authentication}
     * @param userInfoDTO    {@link UserInfoDTO}
     * @return {@link Result<Boolean>}
     */
    @PutMapping
    public Result<Boolean> updateUserInfo(Authentication authentication, @RequestBody @Validated UserInfoDTO userInfoDTO) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(userInfoService.update(userId, userInfoDTO));
    }

    /**
     * 获取单个用户信息
     *
     * @param userId 用户Id
     * @return {@link Result<UserInfoVO>}
     */
    @GetMapping("/{userId}")
    public Result<UserInfoVO> getUserInfoById(@PathVariable("userId") String userId) {
        return ResultUtil.success(UserInfoUtil.convert(userInfoService.getById(userId)));
    }

    /**
     * 获取多个用户信息
     *
     * @param ids   用户Id列表
     * @param nicknames 昵称列表
     * @param asc       正序排序字段
     * @param desc      倒序排序字段
     * @return {@link List<UserInfoVO>}的{@link Result}
     */
    @GetMapping("/list")
    public Result<List<UserInfoVO>> list(@RequestParam(value = "ids", required = false) List<String> ids,
                                         @RequestParam(value = "nicknames", required = false) List<String> nicknames,
                                         @RequestParam(value = "asc", required = false) String[] asc,
                                         @RequestParam(value = "desc", required = false) String[] desc) {
        QueryWrapper<UserInfo> queryWrapper = UserInfoUtil.compose(ids, nicknames, asc, desc);
        return ResultUtil.success(UserInfoUtil.convert(userInfoService.list(queryWrapper)));
    }

    /**
     * 分页获取多个用户信息
     *
     * @param ids   用户Id列表
     * @param nicknames 昵称列表
     * @param asc       正序排序字段
     * @param desc      倒序排序字段
     * @param page      页数
     * @param size      每页大小
     * @return {@link PageResult<UserInfoVO>}的{@link Result}
     */
    @GetMapping("/list/{page}/{size}")
    public Result<PageResult<UserInfoVO>> page(@RequestParam(value = "ids", required = false) List<String> ids,
                                               @RequestParam(value = "nicknames", required = false) List<String> nicknames,
                                               @RequestParam(value = "asc", required = false) String[] asc,
                                               @RequestParam(value = "desc", required = false) String[] desc,
                                               @PathVariable("page") Integer page,
                                               @PathVariable("size") Integer size) {
        IPage<UserInfo> userInfoIPage = IPageUtil.compose(page, size);
        QueryWrapper<UserInfo> queryWrapper = UserInfoUtil.compose(ids, nicknames, asc, desc);
        return ResultUtil.success(UserInfoUtil.convert(userInfoService.page(userInfoIPage, queryWrapper)));
    }
}
